<?php

declare (strict_types=1);

namespace App\Model;

use App\Library\Common;
use App\Service\MomentsReviewService;
use Hyperf\Database\Model\Relations\HasMany;
use Hyperf\Database\Model\Relations\HasOne;
use Hyperf\DbConnection\Db;

/**
 */
class Moment extends Model
{
    public $timestamps = false;
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'fly_moments';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [];

    /**
     * 关联视频文件
     * @return HasOne
     */
    public function with_video_profiles()
    {
        return $this->hasOne(Fileinfo::class, 'fileinfo_id', 'video_file');
    }

    /**
     * 关联文章图片
     * @return HasMany
     */
    public function with_img_profiles()
    {
        return $this->hasMany(MomentsProfile::class, 'moments_id', 'id')
            ->where('file_type', '=', 1)
            ->select(['fileinfo_id', 'moments_id'])
            ->with('with_fileinfo');
    }

    /**
     * 关联商品
     * @return HasMany
     */
    public function with_sku_profiles()
    {
        return $this->hasMany(MomentsProfile::class, 'moments_id', 'id')
            ->where('file_type', '=', 2)
            ->with('with_products')
            ->select(['id', 'moments_id', 'product_id']);
    }

    /**
     * 页面浏览量+1
     * @param int $moments_id
     */
    public function page_view_count(int $moments_id)
    {
        self::query()->where(['id' => $moments_id, 'delete_time' => 0])->update(['view_count' => Db::raw("(view_count+1)")]);
    }

    /**
     * 关联动态主评论：及评论的点赞
     * @return HasMany
     */
    public function with_reviews()
    {
        // 主评论点赞数排序
        return $this->hasMany(MomentsReview::class, 'moments_id', 'id')
            ->where('main_review_id', '=', 0)
            ->where('delete_time', '=', 0)
            ->where('review_type', '=', 1)
            ->orderBy('heart_num', 'desc')// 点赞降序
            ->orderBy('id', 'asc')// 时间升序
            ->with(['with_hearts', 'with_sub_review', 'with_reviewer']);// 主评论关联：点赞用户、子集评论、评论用户
    }

    /**
     * 关联动态点赞用户
     * @return HasMany
     */
    public function with_hearts()
    {
        return $this->hasMany(MomentsReview::class, 'moments_id', 'id')
            ->where('replay_to', '=', 0)
            ->where('delete_time', '=', 0)
            ->where('review_type', '=', MomentsReviewService::TYPE_ZAN)
            ->with(['with_reviewer'])
            ->select(['id', 'reviewer', 'moments_id']);
    }

    /**
     * 关联动态收藏用户
     * @return HasMany
     */
    public function with_stars()
    {
        return $this->hasMany(MomentsReview::class, 'moments_id', 'id')
            ->where('replay_to', '=', 0)
            ->where('delete_time', '=', 0)
            ->where('review_type', '=', MomentsReviewService::TYPE_STAR)
            ->with(['with_reviewer'])
            ->select(['id', 'reviewer', 'moments_id']);
    }

    /**
     * 最新动态审核单
     * @return HasOne
     */
    public function with_curr_applys()
    {
        return $this->hasOne(MomentsApply::class, 'moments_id', 'id')
            ->orderBy('id', 'desc');
    }

    /**
     * 关联店铺
     * @return HasOne
     */
    public function with_shop()
    {
        return $this->hasOne(FlyShop::class, 'id', 'shop_id');
    }

    /**
     * 获取推广配置
     * @return HasMany
     */
    public function with_valid_dr()
    {
        return $this->hasMany(MomentsProfile::class)
            ->with('with_shop_ad')
            ->has('with_shop_ad')
            ->where('file_type', '=', 2);
    }

}